﻿using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Utilities;

namespace Language.Intellisense.Signature.Provider
{
    [Export( typeof( IVsTextViewCreationListener ) )]
    [Name( "Signature Help controller" )]
    [TextViewRole( PredefinedTextViewRoles.Editable )]
    [ContentType( "redscript" )]
    internal sealed class CommandHandler : IVsTextViewCreationListener
    {
        #region Imports

        [Import]
        private IVsEditorAdaptersFactoryService AdapterService { get; set; }

        [Import]
        private ISignatureHelpBroker SignatureHelpBroker { get; set; }

        #endregion Imports

        #region IVsTextViewCreationListener

        void IVsTextViewCreationListener.VsTextViewCreated( IVsTextView textViewAdapter )
        {
            ITextView textView = AdapterService.GetWpfTextView( textViewAdapter );
            if ( textView == null )
                return;

            Func< Intellisense.Signature.CommandHandler> creator = () => new Intellisense.Signature.CommandHandler( textViewAdapter, textView, SignatureHelpBroker );
            textView.Properties.GetOrCreateSingletonProperty( creator );
        }

        #endregion IVsTextViewCreationListener
    }
}
